package character.sprite.model;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import main.gui.dataobjecteditor.DataObjectEditor;

import character.sprite.gui.CharacterSpriteAnimator;
import data.EditorObject;
import data.ObjectDatabase;

public class CharacterSprite extends EditorObject {

	// Data object attributes
	private String display_name, body, head, hair, dress, face;

	// Instance attributes
	private BufferedImage image;
	private boolean changed;

	public CharacterSprite() {
		super();
		this.changed = true;
	}

	public synchronized BufferedImage getThumbnailImage() {
		return this.getFullImage().getSubimage(0,
				CharacterSpriteAnimator.SPRITE_HEIGHT * 2,
				CharacterSpriteAnimator.SPRITE_WIDTH,
				CharacterSpriteAnimator.SPRITE_HEIGHT);
	}

	public synchronized BufferedImage getFullImage() {
		if (changed) {
			CharacterStyle style = (CharacterStyle) ObjectDatabase
					.getInstance().getEditorObject(
							CharacterStyle.class.getCanonicalName(), body);
			BufferedImage bodyImage = null;
			if (style != null)
				bodyImage = style.getFullImage();
			style = (CharacterStyle) ObjectDatabase.getInstance()
					.getEditorObject(CharacterStyle.class.getCanonicalName(),
							hair);
			BufferedImage hairImage = null;
			if (style != null)
				hairImage = style.getFullImage();
			style = (CharacterStyle) ObjectDatabase.getInstance()
					.getEditorObject(CharacterStyle.class.getCanonicalName(),
							head);
			BufferedImage headImage = null;
			if (style != null)
				headImage = style.getFullImage();
			style = (CharacterStyle) ObjectDatabase.getInstance()
					.getEditorObject(CharacterStyle.class.getCanonicalName(),
							face);
			BufferedImage faceImage = null;
			if (style != null)
				faceImage = style.getFullImage();
			style = (CharacterStyle) ObjectDatabase.getInstance()
					.getEditorObject(CharacterStyle.class.getCanonicalName(),
							dress);
			BufferedImage dressImage = null;
			if (style != null)
				dressImage = style.getFullImage();
			image = new BufferedImage(bodyImage.getWidth(), bodyImage
					.getHeight(), BufferedImage.TYPE_INT_ARGB);
			Graphics2D g2d = (Graphics2D) image.getGraphics();
			if (bodyImage != null)
				g2d.drawImage(bodyImage, 0, 0, null);
			if (faceImage != null)
				g2d.drawImage(faceImage, 0, -6, null);
			if (dressImage != null)
				g2d.drawImage(dressImage, 0, 0, null);
			if (hairImage != null)
				g2d.drawImage(hairImage, 0, 0, null);
			if (headImage != null)
				g2d.drawImage(headImage, 0, 0, null);
			this.changed = false;
		}
		return image;
	}

	@Override
	public void init() {
		this.changed = true;
	}

	@Override
	protected void initDefaultData() {
		defaults.put("display_name", "New Character Sprite");
		defaults.put("body", "[[No Body]]");
		defaults.put("head", "[[No Head]]");
		defaults.put("hair", "[[No Hair]]");
		defaults.put("face", "[[No Face]]");
		defaults.put("dress", "[[No Dress]]");
	}

	@Override
	public DataObjectEditor buildDataObjectEditor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DataObjectEditor buildReferenceDataAttributePanel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean validateValue(String attrName, Object value) {
		// TODO Auto-generated method stub
		return false;
	}

}
